# Copyright (c) 2020 Presto Labs Pte. Ltd.
# Author: jingyuan

import subprocess
from absl import app, flags
from concurrent.futures import ThreadPoolExecutor, as_completed


def main(argv):
  futures = {}
  with open(flags.FLAGS.cmd_file, 'r') as cmd_file:
    with open(flags.FLAGS.log_file, 'a+b', buffering=0) as log_file:
      with ThreadPoolExecutor(max_workers=flags.FLAGS.cpu) as executor:
        lines = cmd_file.readlines()
        for line in lines:
          futures[executor.submit(subprocess.run,
                                  line,
                                  stdout=subprocess.PIPE,
                                  stderr=subprocess.STDOUT,
                                  check=False,
                                  shell=True)] = line[:15]
        print(f"Total lines: {len(lines)}")
        for future in as_completed(futures):
          result = future.result()
          print(f"{futures[future]} done")
          log_file.write(result.stdout)


if __name__ == '__main__':
  flags.DEFINE_string('cmd_file', '', '')
  flags.DEFINE_string('log_file', '', '')
  flags.DEFINE_integer('cpu', 2, '')
  app.run(main)
